package com.itheima.info.manger.teachercontroller;

import com.itheima.info.manger.domain.Teacher;
import com.itheima.info.manger.teacherservice.TeacherService;

import java.util.Scanner;

public class TeacherController {
    TeacherService teacherService = new TeacherService();
    Scanner sc = new Scanner(System.in);
    //创建老师对象,用于保存老师的信息
    Teacher teacher = new Teacher();
    public void start() {
        teacherloop:
        while (true){
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师  5.退出");
            String choice = sc.next();
            switch (choice){
                case "1":
                    //System.out.println("添加");
                    //开启老师管理系统添加老师信息
                    addTeacher();
                    break;
                case "2":
                    //System.out.println("删除");
                    deleteTeacher();
                    break;
                case "3":
                    //System.out.println("修改");
                    updateTeacher();
                    break;
                case "4":
                    //System.out.println("查看");
                    showTeacher();
                    break;
                case "5":
                    System.out.println("感谢您使用老师管理系统,再见");
                    break teacherloop;
                default:
                    System.out.println("您的输入有误,请重新输入.");
            }
        }
    }

    private void updateTeacher() {
        //提示用户输入需要修改的编号
        String id;
        while (true){
            System.out.println("请输入您要修改的老师编号:");
            id = sc.next();
            boolean result = teacherService.isExists(id);
            if (result){
                break;
            }else {
                System.out.println("您要修改的老师编号不存在,请重新输入.");
            }
        }
        System.out.println("修改后的老师姓名");
        String name = sc.next();
        System.out.println("修改后的老师年龄");
        String age = sc.next();
        System.out.println("修改后的老师出生日期");
        String birthday = sc.next();
        teacher = new Teacher(id,name,age,birthday);
        boolean result = teacherService.updateTeacher(teacher);
        if(result){
            System.out.println("编号为" + id + "的老师信息修改成功.");
            System.out.println("修改后的姓名是" + name + ",年龄是" + age + ",出生日期是" + birthday);
        }else {
            System.out.println("删除失败.");
        }

    }

    private void deleteTeacher() {
        //提示用户输入需要删除的编号
        String id;
        while (true){
            System.out.println("请输入您要删除的老师编号:");
            id = sc.next();
            boolean result = teacherService.isExists(id);
            if (result){
                break;
            }else {
                System.out.println("您要删除的老师编号不存在,请重新输入.");
            }
        }
        boolean result = teacherService.deleteTeacher(id);
        if(result){
            System.out.println("编号为" + id + "的老师信息成功删除.");
        }else {
            System.out.println("删除失败.");
        }
    }

    private void showTeacher() {
        Teacher[] teachers = teacherService.findAllTeacher();
        if (teachers == null){
            System.out.println("您还没有录入数据,请录入后再查询.");
        }else {
            System.out.println("编号\t姓名\t年龄\t出生日期");
            for (int i = 0; i < teachers.length; i++) {
                if (teachers[i] != null){
                    System.out.println(teachers[i].getId() + "\t\t" + teachers[i].getName() + "\t\t" + teachers[i].getAge() + "\t\t" + teachers[i].getBirthday());
                }
            }
        }
    }

    private void addTeacher() {
        String id;
        while (true){
            System.out.println("请输入老师的编号");
            id = sc.next();
            boolean result = teacherService.isExists(id);
            if (result){
                System.out.println("您输入的编号已存在,请重新输入");
            }else {
                break;
            }
        }
        System.out.println("请输入老师的姓名");
        String name = sc.next();
        System.out.println("请输入老师的年龄");
        String age = sc.next();
        System.out.println("请输入老师的出生日期");
        String birthday = sc.next();
        teacher = new Teacher(id,name,age,birthday);
        boolean result = teacherService.addTeacher(teacher);
        if (result){
            System.out.println("添加成功.");
        }else {
            System.out.println("添加失败.");
        }
    }
}
